﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using BUS;
using DevComponents.DotNetBar;

namespace QLST
{
    public partial class frmKhachHang : Form
    {
        KhachHangBUS cls = new KhachHangBUS();
        KhachHangDTO p = new KhachHangDTO();
        bool flag;
        
        public frmKhachHang()
        {
            InitializeComponent();
        }

        private void frmKhachHang_Load(object sender, EventArgs e)
        {
            ClearTextBox();
            LockTextBox(false);
            LockButton(true);
            LoadKH();
        }

        public void LoadKH()
        {
            gvKhachHang.DataSource = cls.LoadKhachHang();
        }

        private void btnKHThem_Click(object sender, EventArgs e)
        {
            LockButton(false);
            LockTextBox(true);
            ClearTextBox();
            gvKhachHang.Enabled = false;
            txtTenKH.Focus();
        }

        private void btnKHXoa_Click(object sender, EventArgs e)
        {
            p.MaKH  = txtMaKH .Text ;
            cls.Del_KhachHang (p);
            frmKhachHang_Load (sender, e);
        }

        private void btnKHSua_Click(object sender, EventArgs e)
        {
            LockTextBox(true);
            LockButton(false);
            gvKhachHang.Enabled = false;
            txtTenKH.Focus();
        }

        private void btnKHLuu_Click(object sender, EventArgs e)
        {
            if (txtMaKH.Text == "")
            {
                flag = true;
            }
            else if (txtMaKH.Text != "")
            {
                flag = false;
            }
            p.MaKH = txtMaKH.Text;
            p.TenKH = txtTenKH.Text;
            p.DiaChi = txtDiaChiKH.Text;
            p.DienThoai = txtDienThoaiKH.Text;
            if (cbbinhthuong.Checked == true)
            {
                p.LoaiKH = false;

            }
            else
                p.LoaiKH = true;
            p.DiemTichLuy = float.Parse(txtDiemTichLuy.Text);
            cls.IsUp_KhachHang(p, flag);
            frmKhachHang_Load(sender, e);
        }

        private void btnKHHuy_Click(object sender, EventArgs e)
        {
            txtTimKiem.Text = "";
            frmKhachHang_Load (sender, e);
        }

        private void btnKHTim_Click(object sender, EventArgs e)
        {
            bool flag;
            p.MaKH  = txtTimKiem.Text;
            p.TenKH = txtTimKiem.Text;
            if (cbMaKH.Checked   == true)
            {
                flag = true;
            }
            else
            {
                flag = false;
            }
            gvKhachHang.ClearSelection();
            try
            {
                gvKhachHang.DataSource = cls.TimKH(p, flag);
            }
            catch
            {
                MessageBoxEx.Show("Bạn nhập sai Mã Khách Hàng hoặc Tên Khách Hàng", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

        private void gvKhachHang_SelectionChanged(object sender, EventArgs e)
        {
            txtMaKH.Text   = gvKhachHang.CurrentRow.Cells["colMaKH"].Value.ToString();
            txtTenKH .Text  = gvKhachHang.CurrentRow.Cells["colHoTen"].Value.ToString();
            txtDiaChiKH .Text  = gvKhachHang.CurrentRow.Cells["colDiaChi"].Value.ToString();
            txtDienThoaiKH .Text  = gvKhachHang.CurrentRow.Cells["colDienThoai"].Value.ToString();
            txtDiemTichLuy .Text  = gvKhachHang.CurrentRow.Cells["colDiemTichLuy"].Value.ToString();
            if (Convert .ToBoolean (gvKhachHang .CurrentRow .Cells ["colLoaiKhachHang"].Value )==true)
            {
                cbthanthiet.Checked = true;
           
            }
            else
                cbbinhthuong.Checked = true;
            
        }
        private void ClearGV()
        {
            gvKhachHang.ClearSelection();
        }

        private void ClearTextBox()
        {
            txtMaKH.Text = "";
            txtTenKH.Text = "";
            txtDiaChiKH.Text = "";
            txtDienThoaiKH.Text = "";
            txtDiemTichLuy.Text = "";
        }

        private void LockButton(Boolean f)
        {
            btnKHThem.Enabled = f;
            btnKHSua.Enabled = f;
            btnKHXoa.Enabled = f;
            btnKHLuu.Enabled = !f;
            btnKHHuy.Enabled = !f;
        }

        private void LockTextBox(Boolean f)
        {
            txtMaKH.Enabled = f;
            txtTenKH.Enabled = f;
            txtDiaChiKH.Enabled = f;
            txtDienThoaiKH.Enabled = f;
            txtDiemTichLuy.Enabled = f;
            if (f == false)
            {
                txtMaKH.BackColor = Color.LemonChiffon;
                txtTenKH.BackColor = Color.LemonChiffon;
                txtDiaChiKH.BackColor = Color.LemonChiffon;
                txtDienThoaiKH.BackColor = Color.LemonChiffon;
                txtDiemTichLuy.BackColor = Color.LemonChiffon;
            }
            else if (f == true)
            {
                txtMaKH.BackColor = Color.White;
                txtTenKH.BackColor = Color.White;
                txtDiaChiKH.BackColor = Color.White;
                txtDienThoaiKH.BackColor = Color.White;
                txtDiemTichLuy.BackColor = Color.White; ;
            }
        }
       

        private void buttonX1_Click(object sender, EventArgs e)
        {
            frmKhachHang_Load(sender, e);
        }

    }
}
